package com.example.yskjproject.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatButton;
import androidx.recyclerview.widget.RecyclerView;

import com.example.yskjproject.R;
import com.example.yskjproject.model.RechargeRecordModel;

import java.util.List;

public class RechargeAmountAdapter extends RecyclerView.Adapter<RechargeAmountAdapter.ViewHolder> {

    private List<RechargeRecordModel> mRechargeRecordModelList;
    private int selectedPosition = -1;  // 默认没有选中的项
    private OnRechargeAmountSelectedListener mListener;

    // 新增一个接口，用于通知父级界面哪个按钮被选中
    public interface OnRechargeAmountSelectedListener {
        void onRechargeAmountSelected(int id, int type, int price, int gift, int value,String rechargeAmount);
    }

    public RechargeAmountAdapter(List<RechargeRecordModel> rechargeRecordListModels, OnRechargeAmountSelectedListener listener) {
        mRechargeRecordModelList = rechargeRecordListModels;
        mListener = listener;  // 接受回调接口
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recharge_num, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        RechargeRecordModel recordModel = mRechargeRecordModelList.get(position);
        if (recordModel.getType() == 3) {
            holder.rechargeAmount.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        } else {
            holder.rechargeAmount.setCompoundDrawablesWithIntrinsicBounds(R.drawable.token, 0, 0, 0);
        }

        // 设置按钮文本
        holder.rechargeAmount.setText(recordModel.getValue()/100 + "");

        // 根据 selectedPosition 来决定当前按钮是否选中
        if (position == selectedPosition) {
            holder.itemView.setBackgroundResource(R.drawable.shape_rounded_transparent_select); // 选中背景
        } else {
            holder.itemView.setBackgroundResource(R.drawable.shape_rounded_transparent); // 默认背景
        }
        // 设置按钮点击事件
        holder.rechargeAmount.setOnClickListener(v -> {
            selectedPosition = position; // 更新选中位置
            notifyDataSetChanged(); // 刷新 RecyclerView

            // 获取选中的按钮的值，并通过回调将其传递出去
            if (mListener != null) {
                mListener.onRechargeAmountSelected(recordModel.getId(), recordModel.getType(), recordModel.getPrice(), recordModel.getGift(), recordModel.getValue(),holder.rechargeAmount.getText().toString());
            }
        });
    }

    @Override
    public int getItemCount() {
        return mRechargeRecordModelList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final AppCompatButton rechargeAmount;

        public ViewHolder(View itemView) {
            super(itemView);
            rechargeAmount = itemView.findViewById(R.id.btn_recharge_amount);
        }
    }
}

